using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using ImPM.Data.ManagerObjects;
using ImPM.Data.BusinessObjects;
using ImPM.Data.Base;

namespace ImPM.Data.UnitTests
{
	[TestFixture]
    public partial class TaskTests : UNuitTestBase
    {
        protected ITaskManager manager;
		
		public TaskTests()
        {
            manager = managerFactory.GetTaskManager();
        }
		
		protected Task CreateNewTask()
		{
			Task entity = new Task();
			
			// You may need to maually enter this key if there is a constraint violation.
			entity.Id = 72;
			
			entity.TaskName = "Test Test ";
			entity.StartDate = DateTime.Now;
			entity.FinishDate = DateTime.Now;
			entity.ProjectID = 28;
			entity.Duration = DateTime.Now;
			entity.Work = DateTime.Now;
			entity.PredecessorID = 40;
			entity.FixedCost = 51;
			entity.PercentageCompleted = 32;
			entity.WB = "Test Test Test Tes";
			entity.DeadLine = DateTime.Now;
			entity.TaskGUID = "Te";
			entity.Note = "Test Test ";
			entity.Type = default(SByte);
			
			return entity;
		}
		protected Task GetFirstTask()
        {
            IList<Task> entityList = manager.GetAll(1);
            if (entityList.Count == 0)
                Assert.Fail("All tables must have at least one row for unit tests to succeed.");
            return entityList[0];
        }
		
		[Test]
        public void Create()
        {
            try
            {
				Task entity = CreateNewTask();
				
                object result = manager.Save(entity);

                Assert.IsNotNull(result);
            }
            catch(Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        [Test]
        public void Read()
        {
            try
            {
                Task entityA = CreateNewTask();
				manager.Save(entityA);

                Task entityB = manager.GetById(entityA.Id);

                Assert.AreEqual(entityA, entityB);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
		[Test]
		public void Update()
        {
            try
            {
                Task entityA = GetFirstTask();
				entityA.TaskName = "Test Test ";
				
				manager.Update(entityA);

                Task entityB = manager.GetById(entityA.Id);

                Assert.AreEqual(entityA.TaskName, entityB.TaskName);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        [Test]
        public void Delete()
        {
            try
            {
                Task entity = GetFirstTask();
				
                manager.Delete(entity);

                entity = manager.GetById(entity.Id);
                Assert.IsNull(entity);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
	}
}